﻿
/*! \file	CSpinLock.h
 *  \author liuck
 *  \date   2019-07-15
 *  \brief	
 *			
 */
#ifndef __CCSPINLOCK_H__
#define __CCSPINLOCK_H__

#include "cslock.h"


#if (defined(POSIX) || defined(_POSIX_C_SOURCE)) && !defined(__ANDROID__) && !defined(__APPLE__)
#include <pthread.h>
#include <sys/types.h>
#include <errno.h>
#include "interlock.h"

class CSpinLock
	: ILockItem
{
public:
	CSpinLock(){
		pthread_spin_init(&m_spinlock, PTHREAD_PROCESS_SHARED); // 初始化自旋锁，该自旋锁可再不同的进程间使用
	};
	virtual ~CSpinLock(){
		pthread_spin_destroy(&m_spinlock);
	};

	long	Lock(){
		return (0 == pthread_spin_lock(&m_spinlock));
	};
	bool	TryLock(){
		return (0 == pthread_spin_trylock(&m_spinlock));
	};
	long	Unlock(){
		return (0 == pthread_spin_unlock(&m_spinlock));
	};

private:
	pthread_spinlock_t m_spinlock;
};

#else
typedef CCrtSection CSpinLock;

#endif


#endif //__CCSPINLOCK_H__
